var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');
var login =  require('./routes/login');
var phapi = require('./routes/phoneapi');
var checkuser = require('./routes/checkuser');
var loginout=require('./routes/loginout');
var test=require('./routes/test');
var main=require('./routes/main');
var left=require('./routes/left');
var botton=require('./routes/botton');
var top=require('./routes/top');
var roul=require('./routes/roul');
var notion=require('./routes/notion');
var agree=require('./routes/agree');
var testroom=require('./routes/testroom');
var getready=require('./routes/getready');
var testinfo=require('./routes/testinfo');
var submit=require('./routes/submit');
var init=require('./routes/init');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/login', login);
app.use('/phapi',phapi);
app.use('/checkuser',checkuser);
app.use('/loginout',loginout);
app.use('/test',test);
app.use('/main',main);
app.use('/left',left);
app.use('/botton',botton);
app.use('/top',top);
app.use('/roul',roul);
app.use('/notion',notion);
app.use('/agree',agree);
app.use('/testroom',testroom);
app.use('/getready',getready);
app.use('/testinfo',testinfo);
app.use('/submit',submit);
app.use('/init',init);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

app.listen(5000,function(){
    console.log("5000 port on! service up!");
});

module.exports = app;
